//
//  RSWebView+WKNavigationDelegate.m
//  StaffTax
//
//  Created by 侯伟 on 2020/3/13.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSWebView+WKNavigationDelegate.h"

@implementation RSWebView (WKNavigationDelegate)


- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
    NSURL* url = navigationAction.request.URL;
    if(!url){
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    
    if (self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]){
        [self.navigationDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
        return;
    }

    decisionHandler(WKNavigationActionPolicyAllow);
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationResponse:decisionHandler:)]){
        [self.navigationDelegate webView:webView decidePolicyForNavigationResponse:navigationResponse decisionHandler:decisionHandler];
    }else{
        decisionHandler(WKNavigationResponsePolicyAllow);
    }
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    if(!self.progressViewHidden){
        self.progressView.progress = 0.1;
    }
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didStartProvisionalNavigation:)]){
        [self.navigationDelegate webView:webView didStartProvisionalNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didReceiveServerRedirectForProvisionalNavigation:)]){
        [self.navigationDelegate webView:webView didReceiveServerRedirectForProvisionalNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    if(!self.progressViewHidden){
        self.progressViewHidden = YES;
    }
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didFailNavigation:withError:)]){
        [self.navigationDelegate webView:webView didFailNavigation:navigation withError:error];
    }
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
//    [webView evaluateJavaScript:@"document.body.style.backgroundColor=\"#000000\"" completionHandler:nil];
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didCommitNavigation:)]){
        [self.navigationDelegate webView:webView didCommitNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    self.progressViewHidden = YES;
//    [webView evaluateJavaScript:@"document.body.style.backgroundColor=\"#000000\"" completionHandler:nil];
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didFinishNavigation:)]){
        [self.navigationDelegate webView:webView didFinishNavigation:navigation];
    }
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error{
    
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didFailProvisionalNavigation:withError:)]){
        [self.navigationDelegate webView:webView didFailProvisionalNavigation:navigation withError:error];
    }
}

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
    
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webView:didReceiveAuthenticationChallenge:completionHandler:)]){
        [self.navigationDelegate webView:webView didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler];
    }else{
        if(!webView.URL.host){
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,NULL);
            return;
        }
        
        NSString* authenticationMethod = challenge.protectionSpace.authenticationMethod;
        if([authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault] || [authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic] ||
           [authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]){
            
        }else if([authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
        } else {
            completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
        }
    }
}

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{
    
    if(self.navigationDelegate && [self.navigationDelegate respondsToSelector:@selector(webViewWebContentProcessDidTerminate:)]){
        [self.navigationDelegate webViewWebContentProcessDidTerminate:webView];
    }
}

@end
